<?php

class Admin_Table_Auction_Cities extends Apk_Db_Table {

    protected $_name = 'dir_town';
    protected $_primary = 'ID';
    protected $_ERROR = '';
    protected $_lang;

    public function __construct($lang = 'ru') {
        $this->_lang = $lang;
        parent::__construct();
    }

    public function GetError() {
        return $this->_ERROR;
    }

    /*public function GetDataList($page = 1, $sort=null, $page_count=null) {
        $sel = $this->getAdapter()->select()
                ->from($this->_name, array('id', 'name' => 'name_' . $this->_lang))
                ->joinLeft('countries', 'countries.id = cities.id_country', array('country' => 'name_' . $this->_lang))
                ->joinLeft('regions', 'regions.id = cities.id_region', array('region' => 'name_' . $this->_lang));

        if (!is_null($sort) && strlen($sort) > 0) {
            $sel->order($sort);
        }
        else
            $sel->order($this->_name . '.name_' . $this->_lang);


        return $this->setDecorator('simpleDecorator')->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function GetDataRow($id) {
        $sel = $this->getAdapter()->select()
                ->from($this->_name)
                ->where('id = ?', $id);

        return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }
*/
    /* public function GetCountriesList($a = null)
      {
      $ret = array();

      $sel = $this->getAdapter()->select()
      ->from('countries', array('id', 'name' => 'name_'.$this->_lang))
      ->order('name_'.$this->_lang);

      $data = $this->getList($sel)->toArray();

      if(count($data) > 0 && !is_null($a))
      foreach($data as $k => $v)
      if($v['id'] == $a) $data[$k]['__attributes']['active'] = 1;

      return $data;
      } */

    public function GetRegionsList($a = null) {
        $ret = array();

        $sel = $this->getAdapter()->select()
                ->from('dir_region', array('id', 'name', 'koatuu'))
                ->order('name');

        $data = $this->setDecorator('simpleDecorator')->getList($sel)->toArray();

        if (count($data) > 0 && !is_null($a))
            foreach ($data as $k => $v)
                if ($v['id'] == $a)
                    $data[$k]['__attributes']['active'] = 1;

        return $data;
    }

    public function GetRegionKoatuu($id = null) {
        $ret = array();

        $sel = $this->getAdapter()->select()
                ->from('dir_region', array('koatuu'))
                ->where('ID = ?', $id);

        return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

    public function GetDistrictsList($a = null, $region_id = null) {
        $ret = array();

        $sel = $this->getAdapter()->select()
                ->from('dir_district', array('id', 'name', 'koatuu'))
                ->order('name');

        if (!is_null($region_id))
            $sel->where('region_id = ?', $region_id);

        $data = $this->getList($sel)->toArray();

        if (count($data) > 0 && !is_null($a))
            foreach ($data as $k => $v)
                if ($v['id'] == $a)
                    $data[$k]['__attributes']['active'] = 1;

        return $data;
    }

    public function GetDistrictsKoatuu($id = null) {
        $ret = array();

        $sel = $this->getAdapter()->select()
                ->from('dir_district', array('id', 'name', 'koatuu'))
                ->where('id = ?', $id);

        return $this->setDecorator('simpleDecorator')->getList($sel)->getRow(0)->toArray();
    }

    public function GetCitiesList($a = null, $region_id = null, $district_id = null) {
        $ret = array();

        $sel = $this->getAdapter()->select()
                ->from('dir_town', array('id', 'name'))
                ->order('name');

        if (!($district_id > 0 && $region_id > 0))
            return array();

        if (!is_null($district_id))
            $sel->where('district_id = ?', $district_id);
        if (!is_null($region_id))
            $sel->where('region_id = ?', $region_id);

        $data = $this->getList($sel)->toArray();

        if (count($data) > 0 && !is_null($a))
            foreach ($data as $k => $v)
                if ($v['id'] == $a)
                    $data[$k]['__attributes']['active'] = 1;

        return $data;
    }
/*
    public function insert(array $data) {

        $data['name_ru'] = trim($data['name_ru']);
        if ($data['name_ru'] == '') {
            $this->_ERROR = 'Необходимо ввести Название';
            return false;
        }

        parent::insert($data);

        return true;
    }

    public function update(array $data, $id) {
        $data['name_ru'] = trim($data['name_ru']);
        if ($data['name_ru'] == '') {
            $this->_ERROR = 'Необходимо ввести Название';
            return false;
        }

        return parent::update($data, 'id = ' . $id);
    }

    public function remove($items) {
        foreach ($items as $k => $id) {
            parent::delete('id = ' . $id);
        }
        return true;
    }
*/
    
    
    /*     * ******************
     * **  Decorators  ***
     * ******************* */

    public function simpleDecorator($key, $value) {
        switch ($key) {
            case 'public_date':
                $result[$key]['__attributes'] = $this->timestampToArray($value);
                break;
            case 'contacts':
            case 'text':
                $result[$key] = stripslashes($value);
                break;
            case 'koatuu':
                $result[$key] = str_pad($value, 10, "0", STR_PAD_LEFT);
                break;
            default:
                $result[$key] = $value;
                break;
        }
        return $result;
    }

}

?>